package com.ykdl.member.kid.regandlogin;

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.widget.Button;

import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.R.color;
import com.ykdl.member.kid.adapters.MyPagerAdapter;
import com.ykdl.member.kid.fragments.HomeFragment;
import com.ykdl.member.kid.gears.BaseFragmentActivity;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.HomeActivity.MyOnPageChangeListener;
import com.ykdl.member.kid.util.ResoureUtil;
import com.ykdl.member.views.IndexViewPager;

public class RegAndLoginActivity extends BaseFragmentActivity {
	private boolean isexite = true;
	private Button login_tag, regist_tag;

	private IndexViewPager pager;
	private ArrayList<Fragment> fragmentsList;
	private LoginFragment loginFragment = null;
    private PhoneNumberRegisterFragment phoneNumberRegisterFragment = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regactivity_xml);
		ResoureUtil.activityList.add(this);
		inint();
		initViewPager();
	}

	private void initViewPager() {
		// TODO Auto-generated method stub

		fragmentsList = new ArrayList<Fragment>();
		loginFragment = new LoginFragment();
		phoneNumberRegisterFragment=new PhoneNumberRegisterFragment();

		fragmentsList.add(loginFragment);
		fragmentsList.add(phoneNumberRegisterFragment);

		MyPagerAdapter adpater = new MyPagerAdapter(
				getSupportFragmentManager(), fragmentsList);

		pager.setAdapter(adpater);
		// 设置默认是第一页
		pager.setCurrentItem(0);
		// 设置ViewPager的页面切换监听事件，默认清空我的消息页被选中
		pager.setOnPageChangeListener(new MyOnPageChangeListener());
		// ViewPager 预加载的处理（我的消息，我的通知和我的私信都进行预加载）
		pager.setOffscreenPageLimit(1);
	}

	private void inint() {
		// TODO Auto-generated method stub
		login_tag = (Button) findViewById(R.id.login_tag);
		regist_tag = (Button) findViewById(R.id.regist_tag);
		login_tag.setOnClickListener(new MyOnClickListener());
		regist_tag.setOnClickListener(new MyOnClickListener());
		pager = (IndexViewPager) findViewById(R.id.tabpager);
		pager.setScanScroll(true);
	}


	// pager的监听器
	public class MyOnPageChangeListener implements OnPageChangeListener {
		Animation animation = null;

		@Override
		public void onPageSelected(int arg0) {
			switch (arg0) {
			case 0:
				 setSelectedID(R.id.login_tag);
				break;
			case 1:
				 setSelectedID(R.id.regist_tag);
				break;

			default:
				break;
			}
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
	}
	
	//
	public class MyOnClickListener implements View.OnClickListener {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.login_tag: 
				setSelectedID(R.id.login_tag);
				pager.setCurrentItem(0, false);
				break;
			case R.id.regist_tag: 
				setSelectedID(R.id.regist_tag);
				pager.setCurrentItem(1, false);
				break;
			default:
				break;
			}
		}
	}
	
	//----------------------
	public void setSelectedID(int id) {
		
		if (id == R.id.login_tag) {
//			login_tag.setBackground(getResources().getDrawable(R.drawable.left_down));
			login_tag.setBackgroundResource(R.drawable.left_down);
			regist_tag.setBackground(getResources().getDrawable(R.drawable.right_up));
			login_tag.setTextColor(Color.RED);
			regist_tag.setTextColor(Color.WHITE);
		}
		if (id == R.id.regist_tag) {
			login_tag.setBackground(getResources().getDrawable(R.drawable.left_up));
			regist_tag.setBackground(getResources().getDrawable(R.drawable.right_down));
			login_tag.setTextColor(Color.WHITE);
			regist_tag.setTextColor(Color.RED);
		}
		
	}
}